package signalprocessing;


import ikelib.ChannelData;
import ikelib.IntegrateAccelaration;
import ikelib.Median;
import ikelib.PlotColor;
import ikelib.wintime;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JSplitPane;
import java.awt.FlowLayout;
import javax.swing.JInternalFrame;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuBar;
import java.awt.Component;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.UIManager;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.JTabbedPane;

import wcg.stateChangeNotify;
import wcg.winDataBase;
import wcg.winchkgProperty;

/**
 * 
 */

/**
 * @author Toshiharu Ikeda <ikeda.104@gmail.com>
 *
 */
public class SignalProcessingPanel extends SignalProcessingPanelBasic implements stateChangeNotify{
	
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//	PlotColor Col = new PlotColor();
//	private winDataBase	Data = null;  //@
	
	
	/**
	 * 
	 */
	private final JTabbedPane tabbedPaneSignalProcess = new JTabbedPane(JTabbedPane.LEFT);
	private PanelIntegration panelIntegration=null;
	private PanelDecimation  panelDecimation=null;
	private PanelTemperature panelOffsetTemperature=null;
	private PanelDynamicRangeTemp panelDynamicRangeTemp=null; 

	
	/**
	 * 
	 */
	public SignalProcessingPanel() {
		//super();
		setLayout(new BorderLayout(0, 0));
		
		// 下側のステータスパネル
		JPanel panelStatus = new JPanel();
		panelStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		add(panelStatus, BorderLayout.SOUTH);
		panelStatus.setPreferredSize(new Dimension(100, 27));
		panelStatus.setLayout(new BorderLayout(0, 0));
		
		JPanel panelSouthEast = new JPanel();
		panelStatus.add(panelSouthEast, BorderLayout.EAST);
		panelSouthEast.setLayout(new GridLayout(0, 1, 0, 0));
		
		/*--------------------------
		 * カメラボタン
		 */
		JButton btnCamera = new JButton("");
		btnCamera.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//------------------------------------------------------
				// save as ダイアログ
				JFileChooser filechooser = new JFileChooser(ExportImageDir);
				filechooser.setFileSelectionMode( JFileChooser.FILES_ONLY);	      
				FileNameExtensionFilter filter = new FileNameExtensionFilter("png file", "png");
				filechooser.addChoosableFileFilter(filter);
				filechooser.setDialogTitle("Save as");
				File tmp = new File(ExportImageDir + "SP-"+Data.getDispStartTime().fmt2()+".png");
				filechooser.setSelectedFile(tmp);
				//filechooser.ensureFileIsVisible(tmp);
				int selected = filechooser.showSaveDialog(null);
				String filename = null;
				if ( selected == JFileChooser.APPROVE_OPTION ) {
					tmp = filechooser.getSelectedFile();
					if(tmp.exists()){
						if(JOptionPane.NO_OPTION ==  JOptionPane.showConfirmDialog(null, "Over write ?","Confirmation",JOptionPane.YES_NO_OPTION)){
							return ;
						}
					}
					filename = tmp.getAbsolutePath();
					ExportImageDir = new String(tmp.getParentFile().getAbsolutePath()+File.separator);
				}
				else{
					return ;
				}
				// TODO pinｇ出力の画質が悪いのはなんとかならないか
				//Dimension dm = panelIntegration.getPanelView().getSize(); 
				Dimension dm = getPanelViewSize(); 
				double ratex = 1.0;
				double ratey = 1.0;
				
				BufferedImage img = new BufferedImage((int)(dm.width*ratex), (int)(dm.height*ratey), BufferedImage.TYPE_INT_RGB);
				Graphics2D g = img.createGraphics();
				g.scale(ratex,ratey);
				panelViewprintAll(g);
				try {
					ImageIO.write(img, "png", new File(filename));
				} catch (IOException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "File Output Error", "Error", JOptionPane.ERROR_MESSAGE); 
					return;
				}
			}
		});
		//-----------------------------------
		// カメラボタン
		btnCamera.setPreferredSize(new Dimension(25,25));
		btnCamera.setIcon(new ImageIcon(SignalProcessingPanel.class.getResource("/images/camera.png")));
		panelSouthEast.add(btnCamera);
		
		
		
		
		tabbedPaneSignalProcess.setBorder(null);
		add(tabbedPaneSignalProcess, BorderLayout.CENTER);
		
		panelDecimation = new PanelDecimation();
		panelDecimation.setColorMode(PlotColor.COLOR_COLOR_WHITE_BACK);
		tabbedPaneSignalProcess.addTab("Decimation", null, panelDecimation, "Decimation");
		
		panelOffsetTemperature = new PanelTemperature();
		panelOffsetTemperature.setColorMode(PlotColor.COLOR_COLOR_WHITE_BACK);
		tabbedPaneSignalProcess.addTab("Offset vs Temp.", null, panelOffsetTemperature, "Offset vs. Temperature");
		
		
		//------------------------------------
		// タブを追加する。
		panelIntegration = new PanelIntegration();
		tabbedPaneSignalProcess.addTab("Vel.& Disp.", null, panelIntegration, "Calculate Velocity and Displacement from Accelaration");
		panelIntegration.setColorMode(PlotColor.COLOR_COLOR_WHITE_BACK);
		
		panelDynamicRangeTemp = new PanelDynamicRangeTemp();
		tabbedPaneSignalProcess.addTab("DynamicRange vs Temp.", null, panelDynamicRangeTemp, "Dynamic range vs Temperature");
	
		tabbedPaneSignalProcess.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				doGetChannel();
			}
		});

		initial();
	
	}
	
/**
 * 	
 */
	private String ExportImageDir=null;
	/**
	 * プロパティのロードとセーブ
	 * @param save
	 */
	public void LoadSaveProperty(boolean save){
		if(save){
			if(ExportImageDir!=null){
				winchkgProperty.TimeSeriesExportImageDir =  ExportImageDir;
			}
		}
		else{
			ExportImageDir = winchkgProperty.TimeSeriesExportImageDir;
		}
	}
	/**
	 * 
	 */
	private void initial(){
		Col.setColorMode(PlotColor.COLOR_COLOR_WHITE_BACK);
	}
	/**
	 * データを再取得するとき
	 */
	protected void doGetChannel(){
		SignalprocessingPanelInterface c = (SignalprocessingPanelInterface)tabbedPaneSignalProcess.getSelectedComponent();
		c.doGetChannel();
	}
	/**
	 * 印刷するＶＩＥＷのサイズを得る
	 * @return
	 */
	private Dimension getPanelViewSize(){
		SignalprocessingPanelInterface c = (SignalprocessingPanelInterface)tabbedPaneSignalProcess.getSelectedComponent();
		return c.getPanelViewSize();
	}
	/**
	 * ＶＩＥＷに印刷させる
	 * @param g
	 */
	private void panelViewprintAll(Graphics2D g){
		SignalprocessingPanelInterface c = (SignalprocessingPanelInterface)tabbedPaneSignalProcess.getSelectedComponent();
		c.panelViewprintAll(g);
	}
	/**
	 * WINDATABASEをセットする。
	 * @param d
	 */
	public void setwinDataBase(winDataBase d){
		Data=d;
		int max = tabbedPaneSignalProcess.getComponentCount();
		for(int i=0;i<max;i++){
			SignalprocessingPanelInterface c = (SignalprocessingPanelInterface)tabbedPaneSignalProcess.getComponent(i);
			c.setwinDataBase(d);
		}
	}
	/* (non-Javadoc)
	 * @see stateChangeNotify#onStateChange(int)
	 */
	@Override
	public void onStateChange(int hint) {
		
	}
	/* (non-Javadoc)
	 * @see SignalProcessingPanelBasic#updateChannel()
	 */
	@Override
	public
	void updateChannel() {
		
	}
}
